﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebApplication2
{
    public partial class _Default : Page
    {
        public static DataTable Data = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                FillData();
            }
        }

        private void FillData()
        {
            var sql = "select * from Info";
            var dt = DbHelper.GetData(sql);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            FillData();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var Id = GetValues(e.RowIndex, 0);
            var Name = GetValues(e.RowIndex, 1);
            var Sex = GetValues(e.RowIndex, 2);
            var Age = GetValues(e.RowIndex, 3);
            var Remarks = GetValues(e.RowIndex, 4);

            var isOk = int.TryParse(Id, out int id);

            if(isOk)
            {
                var sql = string.Format("update Info set Name = '{0}',Sex ='{1}',Age ='{2}',Remarks ='{3}'where Id ='{4}'", Name, Sex, Age, Remarks, Id);
                DbHelper.AddOr(sql);
            }
            else
            {
                var sql = string.Format("insert into Info(Name,Sex,Age,Remarks)values('{0}','{1}','{2}','{3}')", Name, Sex, Age, Remarks);
                DbHelper.AddOr(sql);
            }
            GridView1.EditIndex = -1;
            FillData();
        }

        private string GetValues(int rowIndex, int v)
        {
            var cell = GridView1.Rows[rowIndex].Cells[v];
            string values = string.Empty;
            if (cell.Controls.Count > 0)
            {
                var t = cell.Controls[0];
                var get = (TextBox)t;
                values = get.Text;
            }
            else
            {
                values = cell.Text;
            }
            return values;
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var Id = GetValues(e.RowIndex, 0);

            var sql = string.Format("delete from Info where Id ='{0}'",Id);
            DbHelper.AddOr(sql);

            GridView1.EditIndex = -1;
            FillData();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {

            GridView1.EditIndex = -1;
            FillData();
        }

        protected void Herty_Click(object sender, EventArgs e)
        {
            var sql = "select * from Info";
            Data = DbHelper.GetData(sql);
            var dt = Data.Copy();
            Data = dt;
            dt.Rows.Add(dt.NewRow());
            GridView1.DataSource = Data;
            GridView1.EditIndex = dt.Rows.Count - 1;
            GridView1.DataSource = Data;
            GridView1.DataBind();
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}